﻿using Common;
using Network.UpdateData;
using System.Net.Sockets;
using System.Threading;

namespace SFJDService.SocketHandle
{
    /// <summary>
    /// 接收到用户界面发送来的报告排号请求
    /// </summary>
    public class PH01RegisterCase : PacketHandle
    {
        public PH01RegisterCase(Socket socket, Packet packet) : base(socket, packet)
        {
            this.socket = socket;
        }

        protected override void Handle()
        {
            //同步数据
            SyncData sync = new SyncData();
            Thread.Sleep(100);
            while (SyncData.IsSynchronizing)
            {
                Thread.Sleep(100);
            }
            //数据同步完成后，看有没有重复的

            //数据同步完成后，开始获取编码后两位
            ClassData data_Max = new ClassData();
            data_Max = Runtime.UsersOnLine.activeUsers[0].Data;
            foreach (ClassActive user in Runtime.UsersOnLine.activeUsers)//获取网络上最大数据
            {
                if(data_Max.ID < user.Data.ID)
                {
                    data_Max = user.Data;
                }
            }
            ClassData data = new ClassData();
            data.DecodingFromString(Message);
            //获取本地数据
            int nn = Runtime.xmlData.GetCodeNumber(data.TimeHappen.Year.ToString(), data.Code) + 1;
            if (nn < 10)
            {
                data.Code = data.Code + "0" + nn.ToString();
            }
            else
            {
                data.Code = data.Code + nn.ToString();
            }

            data.ID = Runtime.xmlData.GetLastID() + 1;//号码算法
            if (data_Max.ID >= data.ID)//号码强制修正（忽略数据同步失败）
            {
                data.ID = data_Max.ID + 1;
            }

            //广播添加本次登记信息
            Launcher.SendMsg2All(typeof(PH01SynGotNetMax).GetDictianaryKey(), data.CodingToString());
            Thread.Sleep(10);
            //给本机界面程序返回登记信息
            this.SendTcpBack(20000 + typeof(PH01RegisterCase).GetDictianaryKey(), Runtime.thisUser.UserGuid, data.CodingToString());
        }
    }
}
